/*
 * MeeleEnemy.cpp
 *  Created on: 06/08/2013
 *      Author: munhra
 */

#include "/Users/munhra/cocos2d-x/RobotRunner/Classes/MeeleEnemy.h"
#include "Player.h"

MeeleEnemy* MeeleEnemy::create(const char *filename) {
	MeeleEnemy *enemy = new MeeleEnemy();
	if (enemy && enemy->initWithFile(filename)) {
		// Set to autorelease
		enemy->autorelease();
		return enemy;
	}
	CC_SAFE_DELETE(enemy);
	return NULL;
}

void MeeleEnemy::initMeeleEnemy(Player *playerReference) {
	CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(
			"enemySheet.plist");

	player = playerReference;
	onJump == false;
	velocity = ccp(0.0, 0.0);
	stop = true;
	defineAnimations();
	CCLog("initMeeleEnemy");
	this->setPosition(ccp(1000, 250));
	this->setScaleX(3.22);
	this->setScaleY(2.87);
}

bool MeeleEnemy::doScanForTarget(){
	//CCLog("Scanning ...");

	if (ccpDistance(player->getPosition(),this->getPosition()) < 10){
		// the hero is on the sight move
		CCLog("Hero Detected ...");
		return true;
	}else{
		return false;
	}
}

bool MeeleEnemy::doMoveToTarget(){
	CCLog("Moving ...");
	this->runAction(this->walkAnimation);

	if (player->getPosition().x > this->getPosition().x){
		forwardMarch = true;
		backwardMarch = false;
		this->setFlipX(true);
	}else{
		forwardMarch = false;
		backwardMarch = true;
		this->setFlipX(false);
	}
}
void MeeleEnemy::doStop(){
	//CCLog("Stop ...");
	forwardMarch = false;
	backwardMarch = false;
	//velocity = ccp(0.0, 0.0);
	velocity.x = 0.0;
	stop = true;
	this->stopAllActions();
}

void MeeleEnemy::doAction(){
	CCLog("Attack !!!!");

}

void MeeleEnemy::defineAnimations() {

	CCLog("defineEnemyAnimations");
	const char *name = "enemy_walk";
	char frameNameBuf[32] = { 0 };

	//walk animation
	CCArray *walkAnimFrames = CCArray::create();
	for (int i = 1; i <= 4; i++) {
		sprintf(frameNameBuf, "%s%d.png", name, i);
		walkAnimFrames->addObject(
				CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
						frameNameBuf));
	}

	CCAnimation *walkAnim = CCAnimation::createWithSpriteFrames(walkAnimFrames,
			0.9f);
	walkAnimation = CCRepeatForever::create(CCAnimate::create(walkAnim));
	walkAnimation->retain();
	CCLog("defineEnemyWalk");

	//stop animation
	CCArray *stopAnimFrames = CCArray::create();
	for (int i = 1; i <= 1; i++) {
		sprintf(frameNameBuf, "%s%d.png", name, i);
		stopAnimFrames->addObject(
				CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
						frameNameBuf));
	}

	CCAnimation *stopAnim = CCAnimation::createWithSpriteFrames(stopAnimFrames,
			0.9f);
	stopAnimation = CCRepeatForever::create(CCAnimate::create(stopAnim));
	stopAnimation->retain();
	CCLog("defineEnemyStop");

	//jump animation
	name = "enemy_walk";

	CCArray *jumpAnimFrames = CCArray::create();
	for (int i = 1; i <= 4; i++) {
		sprintf(frameNameBuf, "%s%d.png", name, i);
		jumpAnimFrames->addObject(
				CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(
						frameNameBuf));
	}

	CCAnimation *jumpAnim = CCAnimation::createWithSpriteFrames(jumpAnimFrames,
			0.9f);
	jumpAnimation = CCRepeatForever::create(CCAnimate::create(jumpAnim));
	jumpAnimation->retain();
	CCLog("defineEnemyJump");
}

CCRect MeeleEnemy::collisionBoundingBox() {
	CCPoint diff = ccpSub(desiredPosition, this->getPosition());
	CCRect returnBoundingBox = this->boundingBox();
	returnBoundingBox.origin.x = returnBoundingBox.origin.x + diff.x;
	returnBoundingBox.origin.y = returnBoundingBox.origin.y + diff.y;
	return returnBoundingBox;
}

void MeeleEnemy::update(float dt) {
	CCPoint gravity = ccp(0.0, -500.0);
	CCPoint gravityStep = ccpMult(gravity, dt);
	CCPoint forwardMove = ccp(2500, 0.0);
	CCPoint backwardMove = ccp(-2500.0, 0.0);
	CCPoint forwardStep = ccpMult(forwardMove, dt); //1
	CCPoint backwardStep = ccpMult(backwardMove, dt);

	velocity = ccpAdd(velocity, gravityStep);
	velocity = ccp(velocity.x * 0.90, velocity.y); //2

	if (backwardMarch) {
		CCPoint jumpForce = ccp(0.0, 250.0);
		float jumpCutoff = 150.0;
		// The chunk of code below check if the player is jumping
		if (mightAsWellJump && onGround) {
			//jumping condition

			if (onJump == false) {
				CCLog("onJump false %f\n", velocity.y);
				onJump = true;
				velocity = ccpAdd(velocity, jumpForce);
			} else {
				CCLog("onJump true %f\n", velocity.y);
				onJump = false;
				mightAsWellJump = false;
				backwardMarch = false;
				velocity.x = 0;
				this->stopAllActions();
				this->runAction(this->stopAnimation);

			}

			//velocity = ccpAdd(velocity, jumpForce);
		} else if (!mightAsWellJump && velocity.y > jumpCutoff) {
			velocity = ccp(velocity.x, jumpCutoff);
		}
		velocity = ccpAdd(velocity, backwardStep);
	}

	if (forwardMarch) {
		CCPoint jumpForce = ccp(0.0, 300.0);
		float jumpCutoff = 150.0;
		// The chunk of code below check if the player is jumping
		if (mightAsWellJump && onGround) {
			//jumping condition
			//CCLog("mightAsWellJump && onGround velocity y %f\n",velocity.y);

			if (onJump == false) {
				CCLog("onJump false %f\n", velocity.y);
				onJump = true;
				velocity = ccpAdd(velocity, jumpForce);
			} else {
				CCLog("onJump true %f\n", velocity.y);
				onJump = false;
				mightAsWellJump = false;
				forwardMarch = false;
				velocity.x = 0;
				this->stopAllActions();
				this->runAction(this->stopAnimation);

			}

		} else if (!mightAsWellJump && velocity.y > jumpCutoff) {
			velocity = ccp(velocity.x, jumpCutoff);
			CCLog("mightAsWellJump && onGround velocity y %f\n", velocity.y);
		}
		velocity = ccpAdd(velocity, forwardStep);
	}

	// limits the bondary of the speed
	CCPoint stepVelocity = ccpMult(velocity, dt);
	desiredPosition = ccpAdd(this->getPosition(), stepVelocity);

	if (doScanForTarget()){
		doMoveToTarget();
	}else{
		doStop();
	}

}




